import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

//@author A0096724W
public class CalendarTable extends Application{
    
    AnchorPane root;
    
    //Used to calculate movement of window by mouse dragging
    protected double xOffset;
    protected double yOffset;
    
    @Override
    public void start(final Stage primaryStage) throws Exception {
	AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("/CalendarTable.fxml"));
	primaryStage.initStyle(StageStyle.TRANSPARENT);
	
	setWindowMovable(primaryStage, root);
	
	Scene scene = new Scene(root);
	scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
	
	setHotKeyForClosingWindow(primaryStage, scene);
	
	primaryStage.setScene(scene);
	primaryStage.show();
    }

    private void setHotKeyForClosingWindow(final Stage primaryStage, Scene scene) {
	scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
	    public void handle(KeyEvent ke) {
	        if (ke.getCode().equals(KeyCode.ESCAPE)) {
	            primaryStage.close();
	        }
	    }
	});
    }

    private void setWindowMovable(final Stage primaryStage, AnchorPane root) {
	root.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
	
	root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setX(event.getScreenX() - xOffset);
                primaryStage.setY(event.getScreenY() - yOffset);
            }
        });
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
